<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ventasController
 *
 * @author MASTER
 */
App::uses('detallesVenta', 'detallesVenta');

class ventasController extends AppController {

    //put your code here
    public $name = "ventas";

    public function beforeFilter() {
        parent::beforeFilter();
        if (($this->Session->check('user')) && ($_SESSION['user'][0]['Tipo'] == 'cliente')) {
            $this->Auth->allow();
        } elseif (($this->Session->check('user')) && ($_SESSION['user'][0]['Tipo'] == 'admin')) {
            $this->Auth->allow();
        }
    }

    public function view($id = null) {
        $this->venta->id = $id;
        $this->venta->recursive = 1;
        $this->set('ventas', $this->venta->read());
        $this->layout = 'ajax';
    }

    public function index() {
        
    }

    public function estadoventas($id, $estado) {
        $this->venta->id = $id;
        $this->venta->saveField("estado", $estado);
        $this->set('ventas', 'ajax');
        $this->layout = 'ajax';
    }

    public function add() {
        $carrito = $_SESSION['carrito']; //PASO ACTUAL CARRITO A UN ARREGLO
        $user = $_SESSION['user'];
        $idu = $user[0]['IdUsu'];
        $totalcarrito = 0;
        for ($i = 0; $i < count($carrito); $i++) {
            $totalcarrito = $carrito[$i]['Precio'] * $carrito[$i]['Cantidad'] + $totalcarrito;
        }
        $hoy = date("d.m.y");
        ;
        $this->venta->create();
        $data = array(
            'venta' => array(
                'fecha' => $hoy,
                'total' => $totalcarrito,
                'estado' => "pendiente",
                'user_id' => $idu,
        ));
        if ($this->venta->save($data)) {
            $ultimaid = $this->venta->getLastInsertId();
            return $this->redirect(array('controller' => 'detallesVentas', 'action' => 'add/' . $ultimaid));
        }
    }

    function listaventas() {
        $this->set('ventas', $this->venta->find('all'));
        $this->layout = 'ajax';
    }

}

?>
